Ext.namespace('GBIF.GeoSpatial')

GBIF.GeoSpatial.WMSCapabilities = function(config){
	
	this.geoMapPanel = new GBIF.GeoSpatial.MapComponent({
				height: 400
			,   width: 500
			,	region : 'center'
			,	border: false
			,	title : "Layer Preview"
	});
	this.geoGridPanel=new GBIF.GeoSpatial.WMSCapabilitiesLayers ({	
			region : 'west'
		,   width: 400
		,	split: true
		, 	autoScroll: true 
		,	border: false
		
	});
	
	this.geoGridPanel.on('addLayer',this.addlayertomap,this);
	
	this.geoGridPanel.on('rowclick',this.loadLayer,this);
	
	Ext.apply(this,config,{
			layout: 'border'
		,	height: '500'
		,	border: false
		,	tbar: [{
						xtype:'label'
					,	style:"padding:0 5px"	
					,	text:'WMS Server URI: '	
					},{
						xtype: 'textfield'  
            		,	fieldLabel: 'WMS Server URI:'
					,	width:350
					,	ref: '../url'
		        	},{
            			text: 'Load'
					,	style:"padding:5px"
					,	handler: this.loadUrl
					, 	scope: this
        			}
					]
		,   proxyUrl: "../../resources/api/getcapabilities-proxy.php?url="
		,	items:[this.geoMapPanel,this.geoGridPanel]

	})
	GBIF.GeoSpatial.WMSCapabilities.superclass.constructor.call(this, config)
}
Ext.extend(GBIF.GeoSpatial.WMSCapabilities,Ext.Panel, {

		loadUrl: function(){
			var url = this.url.getValue();
			this.clearLayer();
			if(url!=''){
					this.geoGridPanel.store.baseParams={
										url: url
										};
					this.geoGridPanel.store.load();
					this.url.setValue('');	
			}else{
				Ext.MessageBox.alert('Error','Please enter url');
			}
			
		}
		
	,	loadLayer:function(grid,index){
			
			var record = grid.getStore().getAt(index);
			var layer = record.get("layer").clone();
			this.clearLayer();
			this.geoMapPanel.map.addLayer(layer);

		}
	,	clearLayer:function(){
			Ext.each(this.geoMapPanel.map.layers,function(maplayer){
					if(maplayer.name != 'World Map' ){
						this.geoMapPanel.map.removeLayer(maplayer);
					}
			},this);
		}
})

